// pages/partyBuilding/partyBuilding.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req: false,
    fromUtil: false,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    activityList: [],
    userInfoId: "",
    tabActive: 0,
    isOrg: false,
    state: "筛选",
    choice: "6",
    hasChange: false,
  },
  choice() {
    this.toggle("isOrg", true);
  },
  orgChoice(e) {
    var choice = e.currentTarget.dataset.choice;
    var value = e.currentTarget.dataset.value;
    if (choice != this.data.choice) {
      this.toggle("hasChange", true);
    } else {
      this.toggle("hasChange", false);
    }
    this.toggle("choice", choice);
    this.toggle("state", value);
    this.activityList(choice);
    this.toggle("isOrg", false);
  },
  // 分页
  onChange(e) {
    var index = e.detail.index;
    this.toggle("tabActive", index);
  },


  user(e) {
    var that = this;
    var id = e.currentTarget.dataset.createuid;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },
  org(e) {
    console.log('org')
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id=' + dataset.orgid
    })
  },
  activityDetail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var actType = e.currentTarget.dataset.acttype;
    var url = "";
    if (type == 1) {
      url = 'pages/register/register?id=' + id;
    } else if (type == 2 && actType != 1) {
      url = '/pages/hundredDetailed/hundredDetailed?id=' + id + "&type=" + actType;
    } else if (type == 2 && actType == 1) {
      url = '/pages/ordinaryDetails/ordinaryDetails?id=' + id;
    }
    wx.navigateToMiniProgram({
      appId: type == 1 ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: type == 1 ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        console.log(res);
      }
    })

  },
  // activityList: function (append) {
  //   var that = this;
  //   var url = that.data.fromUtil ? "orgmini/act/orgActivityList" : "orgmini/act/selectActivity"
  //   var choice = "";
  //   if (append == 6) {
  //     choice = ""
  //   } else {
  //     choice = append
  //   }
  //   console.log(choice);
  //   app.req({
  //     url: url,
  //     data: {
  //       "orgId": that.data.param.orgId,
  //       "type": 0,
  //       "pageNumber": that.data.param.pageNumber,
  //       "pageSize": that.data.param.pageSize,
  //       "choice": choice
  //     },
  //     success: function (res) {
  //       if (200 == res.code) {

  //         var list = res.data.page.rows
  //         for (var item of list) {
  //           if (item.createTime) {
  //             var timestamp = Date.parse(new Date());
  //             var date = new Date(timestamp);
  //             //获取年份  
  //             // var Y = date.getFullYear();
  //             // if (Y == item.createTime.substring(0, 4)) {
  //             //   item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
  //             // } else {
  //             //   item.createTime = item.createTime.substring(0, 4) + '年' + item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
  //             // }
  //             item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
  //           }
  //           if (item.applyEndTime) {
  //             var applyEndTimeStamp = new Date(item.applyEndTime.replace(/-/g, "/")).getTime();
  //             var timestamp = new Date().getTime();
  //             if (applyEndTimeStamp < timestamp) {
  //               item.endApply = true;
  //             } else {
  //               item.endApply = false;
  //             }
  //           } else {
  //             item.endApply = false;
  //           }
  //         }
  //         for (var item of list) {
  //           item.birth = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日'
  //           if (item.actStartTime.substring(0, 10) == item.actEndTime.substring(0, 10)) {
  //             item.actTime = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(11, 16);
  //           } else {
  //             item.actTime = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(5, 7) + '月' + item.actEndTime.substring(8, 10) + '日' + item.actEndTime.substring(11, 16)
  //           }
  //         }
  //         if (!list || list.length < that.data.param.pageSize) {
  //           that.toggle("pageEnd", true);
  //         }
  //         console.log(list, that.data.hasChange)
  //         that.toggle("activityList", !that.data.hasChange ? that.data.activityList.concat(list) : list);
  //         that.toggle("req", true);
  //       }
  //     }
  //   });
  // },

  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.band) {
      return;
    }
    var url = dataset.turn;
    if (dataset.id) {
      url += "?id=" + dataset.id
    }
    console.log(url)
    wx.navigateTo({
      url: url
    })
  },
  // 党建
  otherList: function (append) {
    var that = this;
    that.toggle("req", false);
    var url = '';
    var data = {};
    url = "orgmini/org/list";
    data = {
      "orgId": that.data.param.orgId,
      "messageType": 4,
      "pageNumber": that.data.param.pageNumber,
      "pageSize": that.data.param.pageSize
    }
    app.req({
      url: url,
      data: data,
      success: function (res) {
        if (200 == res.code) {
          var list = res.data.rows
          for (var item of list) {
            if (item.createTime) {
              item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
            }
          }
          if (!list || list.length < that.data.param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("req", true);
        }
      }
    });
    
  },


  // 组织删除
  orgDelete(e) {
    var that = this;
    var content = "";
    if (!this.data.fromUtil) {
      content = "删除活动不影响其他参与者，他们仍然可查看此活动";
    } else {
      content = "请确认是否删除";
    }
    wx.showModal({
      title: '提示',
      content: content,
      confirmText: "确认",
      success(res) {
        if (res.confirm) {
          var type = e.currentTarget.dataset.type;
          var actType = e.currentTarget.dataset.acttype;
          var relationId = e.currentTarget.dataset.id;
          var messageType
          if (!that.data.fromUtil) {
            if ((type == 2 && actType == 1) || type == 1) {
              messageType = 0;
            } else if (type == 2 && actType == 2) {
              messageType = 2;
            } else if (type == 2 && actType == 3) {
              messageType = 3;
            }
          } else {
            if (type == 1) {
              messageType = 0;
            } else if (type == 2 && actType == 2) {
              messageType = 5;
            } else if (type == 2 && actType == 3) {
              messageType = 6;
            } else if (type == 3) {
              messageType = 3;
            } else if (type == 4) {
              messageType = 1;
            } else if (type == 5) {
              messageType = 2;
            } else if (type == 6) {
              messageType = 4;
            }
          }
          app.req({
            url: that.data.fromUtil ? "orgmini/activity/orgDeleteAct" : "orgmini/activity/userDeleteAct",
            data: {
              "type": messageType,
              "actId": that.data.fromUtil ? "" : relationId,
              "relationId": that.data.fromUtil ? relationId : "",
              "orgId": that.data.fromUtil ? that.data.param.orgId : "",
            },
            success: function (res) {
              if (200 == res.code) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                });
                for (var i in that.data.activityList) {
                  if (relationId == that.data.activityList[i].id) {
                    that.data.activityList.splice(i, 1);
                  }
                }
                that.toggle('activityList', that.data.activityList);
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 筛选
  preventTouchMove: function (e) {
  },
  isorghide() {
    this.setData({
      isOrg: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.toggle('param.orgId', options.id);
      this.toggle('fromUtil', true);
      // this.toggle('userInfoId', options.userInfoId);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.toggle("req", false);
      that.otherList();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.toggle("req", false);
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.otherList();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.otherList(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})